<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        
        span {
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <button>点击</button>
    <div></div>
    <span>夏雨荷</span>

    <script>
        // 封装函数obj目标对象target目标位置
        var div = document.querySelector('div');
        var span = document.querySelector('span');

        function animate(obj, target) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function() {

                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer);
                } else {
                    obj.style.left = obj.offsetLeft + 1 + 'px';
                }
            }, 30)
        }
        var btn = document.querySelector('button');

        btn.addEventListener('click', function() {


            animate(span, 400);
        })
    </script>
</body>

</html>